package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Dept;
import com.blb.entity.ProductHandover;
import com.blb.entity.Warehouse;
import com.blb.service.IDeptService;
import com.blb.service.IProductHandoverService;
import com.blb.service.IWarehouseService;
import com.blb.utils.DataTablesPage;
import com.blb.utils.Response;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *生产管理的产品交接
 * @author tb
 * @since 2020-05-29
 */
@Controller
@RequestMapping("/handover")
public class ProductHandoverController {

    @Autowired
    private IProductHandoverService productHandoverService;

    @Autowired
    private IWarehouseService warehouseService;

    @Qualifier("deptServiceImpl")
    @Autowired
    private IDeptService deptService;
    @RequiresPermissions("handover")
    @ResponseBody
    @GetMapping("page")
    public Map<String, Object> page(Integer draw, Integer start, Integer length){
        String company = ShiroUtils.getCompany();
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("iTotalRecords",productHandoverService.getCount(company));
        pageMap.put("iTotalDisplayRecords",productHandoverService.getCount(company));
        pageMap.put("data",productHandoverService.Ilist(start, length,company));
        return pageMap;
    }

    @RequiresPermissions("handover:add")
    @GetMapping("add_page")
    public String addPage(Model model){
        QueryWrapper<Warehouse> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",ShiroUtils.getCompany());
        List<Warehouse> warehouseList = warehouseService.list(wrapper);
        List<Dept> deptList = deptService.queryProduction(ShiroUtils.getCompany());
        model.addAttribute("warehouseList",warehouseList);
        model.addAttribute("deptList",deptList);
        return "production/handover/cp_jjd_add";
    }

    @RequiresPermissions("handover:add")
    @ResponseBody
    @PostMapping("add")
    public Response add(ProductHandover ph){
        ph.setPhStatus("未被查看");
        ph.setCompanyId(ShiroUtils.getCompany());
        productHandoverService.saveAndLog(ph);
       return new Response();
    }



    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions("handover:deleted")
    @ResponseBody
    @RequestMapping("/deletes")
    public Response delete(@RequestBody List<String> ids){
        productHandoverService.removeByIds(ids);
        return new Response();
    }

    /**
     * 模糊查询
     * @param draw
     * @param start
     * @param length
     * @param orderNo
     * @return
     */
    @RequiresPermissions("handover")
    @ResponseBody
    @GetMapping("query")
    public Map<String, Object> query(Integer draw, Integer start, Integer length,String orderNo){
        String company = ShiroUtils.getCompany();
        Map<String, Object> pageMap = new HashMap<>();
        List<ProductHandover> productHandovers = productHandoverService.queryByOrderNo(orderNo,company);
        pageMap.put("iTotalRecords",productHandovers.size());
        pageMap.put("iTotalDisplayRecords",productHandovers.size());
        pageMap.put("data",productHandovers);
        return pageMap;
    }

    /**
     * 查看订单号是否存在
     * @param orderNo
     * @return
     */
    @RequiresPermissions("handover")
    @ResponseBody
    @PostMapping("checkOrderNo")
    public Response checkOrderNo(String orderNo){
        Response res = new Response();
        res.setRs(productHandoverService.checkOrderNo(orderNo)>0);
        return res;
    }
}

